using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GameStateManagement;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Input.Touch;

namespace Kiddy.Screens
{
    class MinNumber : GameScreen
    {
        Texture2D skyBackground;
        Texture2D groundBackground;
        Texture2D cloud1;
        Texture2D cloud2;
        Texture2D blue_Khinhkhi;
        Texture2D red_Khinhkhi;
        Texture2D yellow_Khinhkhi;
        SpriteFont numberFont;


        Vector2 cloud1Position;
        Vector2 cloud2Position;
        Vector2 red_Position;
        Vector2 blue_Position;
        Vector2 yellow_Position;
        Vector2 fontScale;

        private float red_update;
        private float blue_update;
        private float yellow_update;
        private int updateCloud;
        private Random random;
        private float screen_Width_3;
        private bool choose_state1;
        private bool choose_state2;
        private bool choose_state3;

        private int Minnumber;
        private int[] NumberArray;
        private int countNumber;


        public void LoadAsset()
        {
            skyBackground = Load<Texture2D>("Textures/Backgrounds/sky");
            groundBackground = Load<Texture2D>("Textures/Backgrounds/ground_max_min");
            cloud1 = Load<Texture2D>("Textures/Items/cloud1");
            cloud2 = Load<Texture2D>("Textures/Items/cloud2");
            red_Khinhkhi = Load<Texture2D>("Textures/Items/redkinhkhi");
            blue_Khinhkhi = Load<Texture2D>("Textures/Items/bluekhinhkhi");
            yellow_Khinhkhi = Load<Texture2D>("Textures/Items/yellowkhinhkhi");
            numberFont = Load<SpriteFont>("Fonts/MenuFont");

            screen_Width_3 = ScreenManager.Game.GraphicsDevice.Viewport.Width / 3;

            cloud1Position = new Vector2(500, 32);
            cloud2Position = new Vector2(64, 90);
            fontScale = new Vector2(70, 150);


            red_Position = new Vector2(50, 480);
            blue_Position = new Vector2(screen_Width_3 + 50, 480);
            yellow_Position = new Vector2(screen_Width_3 * 2 + 50, 480);


            red_update = -1.0f;
            blue_update = -2.0f;
            yellow_update = -1.5f;
            updateCloud = 1;
            choose_state1 = true;
            choose_state2 = true;
            choose_state3 = true;

            NumberArray = new int[3];
            Minnumber = 0;
            countNumber = 0;

        }

        public override void LoadContent()
        {
            LoadAsset();
            SetNumberArray();
            base.LoadContent();
        }

        private void SetNumberArray()
        {
            random = new Random();
            for (int i = 0; i < 3; i++)
            {
                NumberArray[i] = random.Next(1, 100);
                int j = i;
                while (j != 0 && j < 3)
                {
                    j--;
                    if (NumberArray[i] == NumberArray[j])
                    {
                        j = 3;
                        i = i - 1;
                    }

                }
            }

            SetMaxNumber();
        }

        private void SetMaxNumber()
        {
            Minnumber = NumberArray[0];
            for (int i = 0; i < 3; i++)
            {
                if (NumberArray[i] < Minnumber)
                    Minnumber = NumberArray[i];
            }
        }

        public override void Draw(GameTime gameTime)
        {
            ScreenManager.SpriteBatch.Begin();
            DrawBackground();
            DrawItems();
            float messageWidth = numberFont.MeasureString(NumberArray[0].ToString()).X;
            float messageWidth1 = numberFont.MeasureString(NumberArray[1].ToString()).X;
            float messageWidth2 = numberFont.MeasureString(NumberArray[2].ToString()).X;

            ScreenManager.SpriteBatch.DrawString(
                numberFont,
                NumberArray[0].ToString(),
                new Vector2(red_Position.X + (red_Khinhkhi.Width - messageWidth) / 2, red_Position.Y + red_Khinhkhi.Height / 4),
                Color.White);
            ScreenManager.SpriteBatch.DrawString(
                numberFont,
                NumberArray[1].ToString(),
                new Vector2(blue_Position.X + (blue_Khinhkhi.Width - messageWidth1) / 2, blue_Position.Y + blue_Khinhkhi.Height / 4),
                Color.White);
            ScreenManager.SpriteBatch.DrawString(
                numberFont,
                NumberArray[2].ToString(),
                new Vector2(yellow_Position.X + (yellow_Khinhkhi.Width - messageWidth2) / 2, yellow_Position.Y + yellow_Khinhkhi.Height / 4),
                Color.White);

            ScreenManager.SpriteBatch.End();
            base.Draw(gameTime);
        }

        private void DrawBackground()
        {
            // Clear the background
            ScreenManager.Game.GraphicsDevice.Clear(Color.White);

            // Draw the Sky
            ScreenManager.SpriteBatch.Draw(skyBackground, Vector2.Zero, Color.White);
            // Draw the ground
            ScreenManager.SpriteBatch.Draw(groundBackground, new Vector2(0, ScreenManager.Game.GraphicsDevice.Viewport.Height - groundBackground.Height), Color.White);

            // Draw Cloud #1
            ScreenManager.SpriteBatch.Draw(cloud1, cloud1Position, Color.White);

            // Draw Cloud #2
            ScreenManager.SpriteBatch.Draw(cloud2, cloud2Position, Color.White);
        }

        private void DrawItems()
        {
            ScreenManager.SpriteBatch.Draw(red_Khinhkhi, red_Position, Color.White);
            ScreenManager.SpriteBatch.Draw(blue_Khinhkhi, blue_Position, Color.White);
            ScreenManager.SpriteBatch.Draw(yellow_Khinhkhi, yellow_Position, Color.White);
        }

        public override void Update(GameTime gameTime, bool otherScreenHasFocus, bool coveredByOtherScreen)
        {

            if (cloud1Position.X + cloud1.Width <= 0)
            {
                cloud1Position.X = 800;
            }
            if (cloud2Position.X + cloud2.Width <= 0)
            {
                cloud2Position.X = 800;
            }

            if (red_update != -20.0f && blue_update != -20.0f && yellow_update != -20.0f)
            {
                if (red_Position.Y <= ScreenManager.Game.GraphicsDevice.Viewport.Height / 10)
                {
                    choose_state1 = false;
                    red_update = 0.5f;
                }
                if (blue_Position.Y <= ScreenManager.Game.GraphicsDevice.Viewport.Height / 10)
                {
                    choose_state2 = false;
                    blue_update = 0.5f;
                }
                if (yellow_Position.Y <= ScreenManager.Game.GraphicsDevice.Viewport.Height / 10)
                {
                    choose_state3 = false;
                    yellow_update = 0.5f;
                }
            }

            if (choose_state1 == false || choose_state2 == false || choose_state3 == false)
            {
                if (red_Position.Y == ScreenManager.Game.GraphicsDevice.Viewport.Height / 2)
                {
                    if (choose_state1 == false)
                        red_update = -red_update;
                }
                if (blue_Position.Y == ScreenManager.Game.GraphicsDevice.Viewport.Height / 2)
                {
                    if (choose_state2 == false)
                        blue_update = -blue_update;
                }
                if (yellow_Position.Y == ScreenManager.Game.GraphicsDevice.Viewport.Height / 2)
                {
                    if (choose_state3 == false)
                        yellow_update = -yellow_update;
                }
            }

            cloud1Position.X = cloud1Position.X - updateCloud;
            cloud2Position.X = cloud2Position.X - updateCloud;
            red_Position.Y = red_Position.Y + red_update;
            blue_Position.Y = blue_Position.Y + blue_update;
            yellow_Position.Y = yellow_Position.Y + yellow_update;

            if (red_update != -20.0f && blue_update != -20.0f && yellow_update != -20.0f)
            {
                TouchCollection touches = TouchPanel.GetState();

                if (touches.Count > 0)
                {
                    if (touches[0].Position.Y >= red_Position.Y
                        && touches[0].Position.Y < red_Position.Y + red_Khinhkhi.Height / 2
                        && touches[0].Position.X >= red_Position.X
                        && touches[0].Position.X < red_Position.X + red_Khinhkhi.Width)
                    {
                        countNumber = 0;
                    }

                    else if (touches[0].Position.Y >= blue_Position.Y
                        && touches[0].Position.Y < blue_Position.Y + blue_Khinhkhi.Height / 2
                        && touches[0].Position.X >= blue_Position.X
                        && touches[0].Position.X < blue_Position.X + blue_Khinhkhi.Width)
                    {
                        countNumber = 1;
                    }

                    else if (touches[0].Position.Y >= yellow_Position.Y
                        && touches[0].Position.Y < yellow_Position.Y + yellow_Khinhkhi.Height / 2
                        && touches[0].Position.X >= yellow_Position.X
                        && touches[0].Position.X < yellow_Position.X + yellow_Khinhkhi.Width)
                    {
                        countNumber = 2;
                    }

                    if (NumberArray[countNumber] == Minnumber)
                    {
                        red_update = -20.0f;
                        blue_update = -20.0f;
                        yellow_update = -20.0f;
                        choose_state1 = true;
                        choose_state2 = true;
                        choose_state3 = true;
                    }

                }
            }

            if (red_update == -20.0f || blue_update == -20.0f || yellow_update == -20.0f)
            {
                if (red_Position.Y < -red_Khinhkhi.Height)
                {
                    red_Position = new Vector2(50, 480);
                    red_update = -1.0f;

                }
                if (blue_Position.Y < -blue_Khinhkhi.Height)
                {
                    blue_Position = new Vector2(screen_Width_3 + 50, 480);
                    blue_update = -2.0f;

                }
                if (yellow_Position.Y < -yellow_Khinhkhi.Height)
                {
                    yellow_Position = new Vector2(screen_Width_3 * 2 + 50, 480);
                    yellow_update = -1.5f;
                }

                if (red_update != -20.0f && blue_update != -20.0f && yellow_update != -20.0f)
                {
                    SetNumberArray();
                }
            }
            base.Update(gameTime, otherScreenHasFocus, coveredByOtherScreen);
        }
    }
}
